package video;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import video.vo.PropertiesModel;
import video.vo.favorites.BiliFavoritesRespVO;
import video.vo.favorites.BiliInfoFavoritesModel;
import video.vo.up.BiliInfoModel;

import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 下载  收藏夹 封面图片文件 和 生成下载的bat文件
 * @author 胡琦
 * @date 2022-04-15 星期五 22:18
 */
public class CreateBilibiliFavoritesDownFileDemo {
    /** 封面图片存放的文件夹 */
    private final static String GRAPH_FOLDER = "\\img\\";

    /** 文件存放的位置 */
    private final static String FILE_PRE = "D:\\视频\\UP视频\\故事";
    /** 需要解析的 json 字符串*/
    private final static String JSON = "{\"code\":0,\"message\":\"0\",\"ttl\":1,\"data\":{\"info\":{\"id\":1272957319,\"fid\":12729573,\"mid\":106794019,\"attr\":23,\"title\":\"故事\",\"cover\":\"http://i1.hdslb.com/bfs/archive/d6d28472a7d725c619bb97241d30a0eb7d2f9b4d.jpg\",\"upper\":{\"mid\":106794019,\"name\":\"琦_404\",\"face\":\"http://i1.hdslb.com/bfs/face/379121e7afb83bbf25571baa99775b28ab1a8818.jpg\",\"followed\":false,\"vip_type\":2,\"vip_statue\":1},\"cover_type\":2,\"cnt_info\":{\"collect\":0,\"play\":371,\"thumb_up\":0,\"share\":0},\"type\":11,\"intro\":\"\",\"ctime\":1624806290,\"mtime\":1624806290,\"state\":0,\"fav_state\":0,\"like_state\":0,\"media_count\":11},\"medias\":[{\"id\":252943591,\"type\":2,\"title\":\"表白是说明心意，而不是索取关系\",\"cover\":\"http://i1.hdslb.com/bfs/archive/d6d28472a7d725c619bb97241d30a0eb7d2f9b4d.jpg\",\"intro\":\"\",\"page\":1,\"duration\":125,\"upper\":{\"mid\":688526775,\"name\":\"TA说故事\",\"face\":\"http://i0.hdslb.com/bfs/face/b3e89328c50589b306b9b85a0d057b4360fc4f4d.jpg\"},\"attr\":0,\"cnt_info\":{\"collect\":63,\"play\":14803,\"danmaku\":12},\"link\":\"bilibili://video/252943591\",\"ctime\":1641463997,\"pubtime\":1641463997,\"fav_time\":1641464356,\"bv_id\":\"BV1dY411a7Hg\",\"bvid\":\"BV1dY411a7Hg\",\"season\":null,\"ogv\":null,\"ugc\":{\"first_cid\":478694608}},{\"id\":548323263,\"type\":2,\"title\":\"爱不是一物降一物～\",\"cover\":\"http://i1.hdslb.com/bfs/archive/c574580f4aef0a93bd45e4190d82e4b5548ccb21.jpg\",\"intro\":\"转载\",\"page\":1,\"duration\":32,\"upper\":{\"mid\":500149620,\"name\":\"季凌说\",\"face\":\"http://i0.hdslb.com/bfs/face/5d873abf0c8118b6da817449812d21aebe5eaeb4.jpg\"},\"attr\":0,\"cnt_info\":{\"collect\":529,\"play\":145704,\"danmaku\":15},\"link\":\"bilibili://video/548323263\",\"ctime\":1632823682,\"pubtime\":1632823681,\"fav_time\":1635656071,\"bv_id\":\"BV1dq4y1P7QZ\",\"bvid\":\"BV1dq4y1P7QZ\",\"season\":null,\"ogv\":null,\"ugc\":{\"first_cid\":415843694}},{\"id\":291172770,\"type\":2,\"title\":\"“他们看起来是一对情侣”\",\"cover\":\"http://i1.hdslb.com/bfs/archive/6846efb9292a884ce62a0154761cbaf413c1c623.jpg\",\"intro\":\"\",\"page\":1,\"duration\":43,\"upper\":{\"mid\":2012936810,\"name\":\"我们都有问题-\",\"face\":\"http://i2.hdslb.com/bfs/face/6a3d43c5c45ef3ad96858fc31ee0879e077567fc.jpg\"},\"attr\":0,\"cnt_info\":{\"collect\":2447,\"play\":265554,\"danmaku\":79},\"link\":\"bilibili://video/291172770\",\"ctime\":1623839782,\"pubtime\":1623839782,\"fav_time\":1634790180,\"bv_id\":\"BV1zf4y187eU\",\"bvid\":\"BV1zf4y187eU\",\"season\":null,\"ogv\":null,\"ugc\":{\"first_cid\":355165756}},{\"id\":335936410,\"type\":2,\"title\":\"“记得刚开始我连你叫什么都不感兴趣”\",\"cover\":\"http://i2.hdslb.com/bfs/archive/3efa26e2916689ef7046019be14acfb7c6431c99.jpg\",\"intro\":\"\",\"page\":1,\"duration\":48,\"upper\":{\"mid\":2012936810,\"name\":\"我们都有问题-\",\"face\":\"http://i2.hdslb.com/bfs/face/6a3d43c5c45ef3ad96858fc31ee0879e077567fc.jpg\"},\"attr\":0,\"cnt_info\":{\"collect\":76,\"play\":34236,\"danmaku\":8},\"link\":\"bilibili://video/335936410\",\"ctime\":1633574033,\"pubtime\":1633574033,\"fav_time\":1634565705,\"bv_id\":\"BV1UR4y1n7UU\",\"bvid\":\"BV1UR4y1n7UU\",\"season\":null,\"ogv\":null,\"ugc\":{\"first_cid\":421410491}},{\"id\":931473283,\"type\":2,\"title\":\"“搞怪的不是红绿灯，不是时机，而是我数不清的犹豫。”\",\"cover\":\"http://i1.hdslb.com/bfs/archive/06ef590dca7d947a6ecd4536bb1b0c523ff85341.jpg\",\"intro\":\"\",\"page\":1,\"duration\":25,\"upper\":{\"mid\":2012936810,\"name\":\"我们都有问题-\",\"face\":\"http://i2.hdslb.com/bfs/face/6a3d43c5c45ef3ad96858fc31ee0879e077567fc.jpg\"},\"attr\":0,\"cnt_info\":{\"collect\":97,\"play\":40405,\"danmaku\":15},\"link\":\"bilibili://video/931473283\",\"ctime\":1625468942,\"pubtime\":1625468942,\"fav_time\":1628038689,\"bv_id\":\"BV1NM4y1M7o5\",\"bvid\":\"BV1NM4y1M7o5\",\"season\":null,\"ogv\":null,\"ugc\":{\"first_cid\":364694734}},{\"id\":458752361,\"type\":2,\"title\":\"看你一眼就会笑的人，一定特别喜欢你\",\"cover\":\"http://i0.hdslb.com/bfs/archive/9690be8f6385bf3374ab0d9574ecd1769912b26b.jpg\",\"intro\":\"\",\"page\":1,\"duration\":108,\"upper\":{\"mid\":688526775,\"name\":\"TA说故事\",\"face\":\"http://i0.hdslb.com/bfs/face/b3e89328c50589b306b9b85a0d057b4360fc4f4d.jpg\"},\"attr\":0,\"cnt_info\":{\"collect\":649,\"play\":281538,\"danmaku\":212},\"link\":\"bilibili://video/458752361\",\"ctime\":1611661803,\"pubtime\":1611661801,\"fav_time\":1625929771,\"bv_id\":\"BV1k5411E7dw\",\"bvid\":\"BV1k5411E7dw\",\"season\":null,\"ogv\":null,\"ugc\":{\"first_cid\":287748460}},{\"id\":970810654,\"type\":2,\"title\":\"如果穿过了柴米油盐酱醋茶还爱着你 那便是真的爱你。\",\"cover\":\"http://i2.hdslb.com/bfs/archive/07091f2be74b4580786ff31bae92c8ae84ae4dfe.jpg\",\"intro\":\"-\",\"page\":1,\"duration\":31,\"upper\":{\"mid\":690790505,\"name\":\"张茄子qiezi\",\"face\":\"http://i2.hdslb.com/bfs/face/995f844b1a8e529605226a8df50d70c5d3681119.jpg\"},\"attr\":0,\"cnt_info\":{\"collect\":1617,\"play\":374906,\"danmaku\":170},\"link\":\"bilibili://video/970810654\",\"ctime\":1609143613,\"pubtime\":1609143613,\"fav_time\":1625403507,\"bv_id\":\"BV1Pp4y1B7UT\",\"bvid\":\"BV1Pp4y1B7UT\",\"season\":null,\"ogv\":null,\"ugc\":{\"first_cid\":271978937}},{\"id\":628063711,\"type\":2,\"title\":\"我以为我已经忘记你的时候，你总有办法告诉我——“并没有”\",\"cover\":\"http://i0.hdslb.com/bfs/archive/777a7ed2e1ddc2a13d61373e4d60650ac0365745.jpg\",\"intro\":\"-\",\"page\":1,\"duration\":23,\"upper\":{\"mid\":690790505,\"name\":\"张茄子qiezi\",\"face\":\"http://i2.hdslb.com/bfs/face/995f844b1a8e529605226a8df50d70c5d3681119.jpg\"},\"attr\":0,\"cnt_info\":{\"collect\":1489,\"play\":444273,\"danmaku\":117},\"link\":\"bilibili://video/628063711\",\"ctime\":1607496740,\"pubtime\":1607496740,\"fav_time\":1625403507,\"bv_id\":\"BV1vt4y1Y7rb\",\"bvid\":\"BV1vt4y1Y7rb\",\"season\":null,\"ogv\":null,\"ugc\":{\"first_cid\":264594640}},{\"id\":204766042,\"type\":2,\"title\":\"两个互相吸引的人，是极致的浪漫\",\"cover\":\"http://i2.hdslb.com/bfs/archive/d50ae781b9d191a8dfb32cba77d4cd82b1d1f921.jpg\",\"intro\":\"\",\"page\":1,\"duration\":97,\"upper\":{\"mid\":688526775,\"name\":\"TA说故事\",\"face\":\"http://i0.hdslb.com/bfs/face/b3e89328c50589b306b9b85a0d057b4360fc4f4d.jpg\"},\"attr\":0,\"cnt_info\":{\"collect\":102,\"play\":58233,\"danmaku\":49},\"link\":\"bilibili://video/204766042\",\"ctime\":1616739977,\"pubtime\":1617018600,\"fav_time\":1625403507,\"bv_id\":\"BV1Bh411S76W\",\"bvid\":\"BV1Bh411S76W\",\"season\":null,\"ogv\":null,\"ugc\":{\"first_cid\":315374254}},{\"id\":460449697,\"type\":2,\"title\":\"最美的不是下雨天\",\"cover\":\"http://i2.hdslb.com/bfs/archive/d21ab66fe02fe203ee070f085d84c855137f9e11.jpg\",\"intro\":\"-\",\"page\":1,\"duration\":79,\"upper\":{\"mid\":690790505,\"name\":\"张茄子qiezi\",\"face\":\"http://i2.hdslb.com/bfs/face/995f844b1a8e529605226a8df50d70c5d3681119.jpg\"},\"attr\":0,\"cnt_info\":{\"collect\":682,\"play\":105470,\"danmaku\":230},\"link\":\"bilibili://video/460449697\",\"ctime\":1620270952,\"pubtime\":1620270952,\"fav_time\":1625403507,\"bv_id\":\"BV1K541137fY\",\"bvid\":\"BV1K541137fY\",\"season\":null,\"ogv\":null,\"ugc\":{\"first_cid\":334511081}},{\"id\":969657661,\"type\":2,\"title\":\"原来我喜欢的他，也在偷偷喜欢我！\",\"cover\":\"http://i2.hdslb.com/bfs/archive/bd1b9506506c09a27879e3c045a35e624a922fea.jpg\",\"intro\":\"\",\"page\":1,\"duration\":72,\"upper\":{\"mid\":688526775,\"name\":\"TA说故事\",\"face\":\"http://i0.hdslb.com/bfs/face/b3e89328c50589b306b9b85a0d057b4360fc4f4d.jpg\"},\"attr\":0,\"cnt_info\":{\"collect\":1429,\"play\":261564,\"danmaku\":113},\"link\":\"bilibili://video/969657661\",\"ctime\":1600929849,\"pubtime\":1600929849,\"fav_time\":1625403385,\"bv_id\":\"BV1Sp4y1e7rm\",\"bvid\":\"BV1Sp4y1e7rm\",\"season\":null,\"ogv\":null,\"ugc\":{\"first_cid\":238496887}}],\"has_more\":false}}";

    public static void main(String[] args) throws IOException {
        PropertiesModel properties = PropertiesModel.builder().filePre(FILE_PRE).json(JSON).build();
        // 解析json
        BiliFavoritesRespVO bModelList = JSONUtil.toBean(properties.getJson(), BiliFavoritesRespVO.class);
        // 解析异常就会空指针
        List<BiliInfoFavoritesModel> medias = bModelList.getData().getMedias();

        List<BiliInfoModel>  vlist = medias.stream().map(x -> {
            BiliInfoModel model = BeanUtil.toBean(x, BiliInfoModel.class);
            model.setPic(x.getCover());
            return model;
        }).collect(Collectors.toList());
        DownloadPictureTest.createBatFile(vlist, properties);
        // 下载所有封面图片
        DownloadPictureTest.downImages(vlist, properties.getFilePre() + GRAPH_FOLDER);
        System.out.println("!!!!   SUCCESS   !!!!!");
    }
}
